<template>
    <div class="center">
      <br />
      <h2  >订单历史记录</h2>
      <br />
      <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="email" label="用户账号" width="220">
        </el-table-column>
        <el-table-column prop="orderId" label="订单编号" width="220">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名字" width="220">
        </el-table-column>
        <el-table-column prop="createTime" label="订单创建日期" width="220">
        </el-table-column>
        <el-table-column prop="orderStatus" label="订单状态" width="220">
        </el-table-column>
        <el-table-column prop="orderPrice" label="订单金额"> </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>

import router from "../router";
export default {
  data() {
    return {
      tableData: [
        // {
        //   orderId: "",
        //   createTime: "",
        //   user: "",
        //   orderStatus: "",
        //   orderPrice: "",
        // },
      ],
      user: "",
    };
  },
  //钩子函数，用于查看是否有秒杀活动,并且实时更新库存
  mounted() {
    //获取参与秒杀的商品名称
    this.getSeckillGoods();
  },
  methods: {
    //时间转换(js将 “2021-07-06T06:23:57.000+00:00” 转换为年月日时分秒)
    transformTimestamp(timestamp) {
      let a = new Date(timestamp).getTime();
      const date = new Date(a);
      const Y = date.getFullYear() + "-";
      const M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      const D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + "  ";
      const h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      const m =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      // const s = date.getSeconds(); // 秒
      const dateString = Y + M + D + h + m;
      // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
      return dateString;
    },
    //获取该用户的秒杀订单信息
    getSeckillGoods() {
      this.$axios
        .get("/seckill/getSecOrdersByUser", {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          if (res.data.extend.result == "用户不存在！") {
            this.$message.error("请先登录");
            router.push("/index");
          } else {
            var resData = res.data.extend.orders;
            
            for (var i = 0; i < resData.length; i++) {
                  resData[i].orderId = resData[i].OrderId ;
              resData[i].orderPrice = resData[i].order_price + "元";
              resData[i].createTime = this.transformTimestamp(
                resData[i].create_time
              );
              if (resData[i].order_status == "1") {
                resData[i].orderStatus = "订单进行中";
              } else if (resData[i].order_status == "2") {
                resData[i].orderStatus = "订单异常";
              } else if (resData[i].order_status == "0") {
                resData[i].orderStatus = "订单已完成";
              }
            }
            
            this.tableData=resData;

            
          }
        });
    },
  },
};

</script>

<style>
.center {
  text-align: center;
}
</style>